<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>服务器IP配置</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .config-container {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            max-width: 500px;
            width: 100%;
        }

        .title {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 24px;
            font-weight: 600;
        }

        .form-group {
            margin-bottom: 25px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        input[type="text"] {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s;
            margin-bottom: 15px;
        }

        .btn:hover {
            transform: translateY(-2px);
        }

        .btn:active {
            transform: translateY(0);
        }

        .btn-secondary {
            background: #6c757d;
        }

        .status {
            text-align: center;
            padding: 10px;
            border-radius: 8px;
            margin-top: 20px;
            font-weight: 500;
        }

        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .current-config {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .current-config h3 {
            color: #495057;
            margin-bottom: 10px;
            font-size: 16px;
        }

        .current-config p {
            color: #6c757d;
            margin: 5px 0;
        }

        .quick-options {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-bottom: 20px;
        }

        .quick-btn {
            padding: 10px;
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            cursor: pointer;
            text-align: center;
            font-size: 14px;
            transition: all 0.2s;
        }

        .quick-btn:hover {
            background: #e9ecef;
            border-color: #adb5bd;
        }
    </style>
</head>
<body>
    <div class="config-container">
        <h1 class="title">WebSocket服务器配置</h1>
        
        <div class="current-config">
            <h3>当前配置</h3>
            <p>服务器地址: <span id="current-url">-</span></p>
            <p>连接状态: <span id="connection-status">未连接</span></p>
        </div>

        <div class="form-group">
            <label for="server-ip">服务器IP地址:</label>
            <input type="text" id="server-ip" placeholder="例如: 192.168.1.3" value="">
        </div>

        <div class="quick-options">
            <div class="quick-btn" onclick="setQuickIP('192.168.1.3')">192.168.1.3</div>
            <div class="quick-btn" onclick="setQuickIP('localhost')">localhost</div>
            <div class="quick-btn" onclick="setQuickIP('192.168.0.100')">192.168.0.100</div>
            <div class="quick-btn" onclick="setQuickIP('127.0.0.1')">127.0.0.1</div>
        </div>

        <button class="btn" onclick="testConnection()">测试连接</button>
        <button class="btn" onclick="saveAndRedirect()">保存并返回</button>
        <button class="btn btn-secondary" onclick="goBack()">返回主页</button>

        <div id="status" class="status" style="display: none;"></div>
    </div>

    <script>
        // 页面加载时显示当前配置
        window.onload = function() {
            const savedIP = localStorage.getItem('websocket_server_ip') || 'localhost';
            document.getElementById('server-ip').value = savedIP;
            document.getElementById('current-url').textContent = `ws://${savedIP}:9090`;
        };

        function setQuickIP(ip) {
            document.getElementById('server-ip').value = ip;
        }

        function showStatus(message, isSuccess) {
            const statusEl = document.getElementById('status');
            statusEl.textContent = message;
            statusEl.className = `status ${isSuccess ? 'success' : 'error'}`;
            statusEl.style.display = 'block';
        }

        function testConnection() {
            const ip = document.getElementById('server-ip').value.trim();
            if (!ip) {
                showStatus('请输入服务器IP地址', false);
                return;
            }

            const wsUrl = `ws://${ip}:9090`;
            showStatus('正在测试连接...', true);

            const testWs = new WebSocket(wsUrl);
            
            const timeout = setTimeout(() => {
                testWs.close();
                showStatus('连接超时，请检查IP地址和网络', false);
            }, 5000);

            testWs.onopen = function() {
                clearTimeout(timeout);
                testWs.close();
                showStatus('连接成功！', true);
                document.getElementById('connection-status').textContent = '连接成功';
            };

            testWs.onerror = function() {
                clearTimeout(timeout);
                showStatus('连接失败，请检查IP地址和服务器状态', false);
                document.getElementById('connection-status').textContent = '连接失败';
            };
        }

        function saveAndRedirect() {
            const ip = document.getElementById('server-ip').value.trim();
            if (!ip) {
                showStatus('请输入服务器IP地址', false);
                return;
            }

            localStorage.setItem('websocket_server_ip', ip);
            showStatus('配置已保存', true);
            
            setTimeout(() => {
                // 根据设备类型跳转到相应页面
                const userAgent = navigator.userAgent.toLowerCase();
                const isMobile = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
                
                if (isMobile) {
                    window.location.href = 'mobile-control.html';
                } else {
                    window.location.href = 'index.html';
                }
            }, 1000);
        }

        function goBack() {
            window.history.back();
        }
    </script>
</body>
</html>